import 'package:design_mode_flutter/adapter_mode/object_adapter/veggie.dart';
import 'package:flutter/material.dart';

///实现
class VeggieList extends StatefulWidget {
  final IVeggiesAdapter adapter;

  VeggieList({required this.adapter});

  @override
  State<VeggieList> createState() => _VeggieListState();
}

class _VeggieListState extends State<VeggieList> {
  final List<Veggie> veggies = [];

  @override
  void initState() {
    _getVeggies();
    super.initState();
  }

  void _getVeggies() {
    setState(() {
      veggies.addAll(widget.adapter.getVeggies());
    });
  }

  @override
  Widget build(BuildContext context) {
    return veggies.isEmpty
        ? const Text("无水果")
        : Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              for (var veggie in veggies) ListTile(title: Text(veggie.name))
            ],
          );
  }
}

class AdapterExample extends StatelessWidget {
  const AdapterExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: ScrollConfiguration(
          behavior: const ScrollBehavior(),
          child: SingleChildScrollView(
            child: VeggieList(
              adapter: JsonVeggiesAdapter(),
            ),
          ),
        ),
      ),
    );
  }
}

main() {
  runApp(
    MaterialApp(
      home: AdapterExample(),
    ),
  );
}
